# Mocks to make development more convenient
angular.module('jl-customer-dev', ['jl-customer', 'ngMockE2E'])

.run ($httpBackend) ->
    $httpBackend.whenPOST(/customer\/login$/).respond (method, url, data) ->
        data = angular.fromJson data
        if data.password == '123'
            [200, {
                status: 0
                error: ""
                body: {status: 0 , accessToken: "mocktoken"}
            }]
        else
            [200, {status: 0, body: {reason: "用户名或密码输入错误"}}]

    $httpBackend.whenPOST(/customer\/register$/).respond (method, url, data) ->
        [200, {
            status: 0
            error: ""
            body: {status: 0 , token: "mocktoken"}
        }]

    $httpBackend.whenGET(/customer\/token_valid$/).respond (method, url, data) ->
        [200, {status: 0}]

    $httpBackend.whenPOST(/api\/logout/).respond (method, url, data) ->
        [200]

    addrList = [{
        id: 1,
        address: "天等路xxx号xxx",
        isDefault: true,
        phone: "15555555555",
        city: "上海市",
        district: "浦东新区",
        province: "上海市",
        name: "王二小",
        cityCode: 330000,
        districtCode: 330000,
        provinceCode: 330000
    }, {
        id: 2,
        address: "abc路ddd号ccc",
        isDefault: false,
        phone: "13333333333",
        city: "武汉市",
        district: "芜湖区",
        province: "湖北省",
        name: "王二小",
        cityCode: 330000,
        districtCode: 330000,
        provinceCode: 330000
    }]

    $httpBackend.whenGET(/customer\/address$/).respond (method, url, data) ->
        [200, {status: 0, error: "", body: addrList}]

    $httpBackend.whenPOST(/customer\/address$/).respond (method, url, data) ->
        data = angular.fromJson data
        if data.isDefault # TODO 由后端确认重置其他地址的isDefault
            _.each addrList, (_addr) -> _addr.isDefault = false
        data.id = parseInt(_.last(addrList).id) + 1
        addrList.push data
        [200, {status: 0}]

    $httpBackend.whenGET(/customer\/address\/\d+$/).respond (method, url, data) ->
        id = parseInt url.match(/\d+$/)[0]
        addr = _.find addrList, (_addr) -> _addr.id == id
        [200, {status: 0, error: "", body: addr}]

    $httpBackend.whenPUT(/customer\/address\/\d+$/).respond (method, url, data) ->
        data = angular.fromJson data
        if data.isDefault # TODO 由后端确认重置其他地址的isDefault
            _.each addrList, (_addr) -> _addr.isDefault = false
        id = parseInt url.match(/\d+$/)[0]
        addr = _.find addrList, (_addr) -> _addr.id == id
        _.extend addr, data
        [200, {status: 0}]

    $httpBackend.whenDELETE(/customer\/address\/\d+$/).respond (method, url, data) ->
        id = parseInt url.match(/\d+$/)[0]
        addrList = _.filter addrList, (_addr) -> _addr.id != id # 从addrList中删除指定id的元素
        [200, {status: 0}]

    cardList = [{
        city: ""
        cityCode: 0
        description: "折扣"
        discountType: 2
        discountValue: 0.9
        discountValueDisplay: "10"
        expireDate: "2015-06-03"
        id: 2
        isExpire: false
        isGlobal: true
        isUsed: true
        name: "六一折扣优惠券"
        statusDisplay: "已使用"
        url: ""
    },{
        city: ""
        cityCode: 0
        description: "折扣"
        discountType: 1
        discountValue: 50
        discountValueDisplay: "10"
        expireDate: "2015-06-03"
        id: 3
        isExpire: false
        isGlobal: true
        isUsed: true
        name: "六一折扣抵用券"
        statusDisplay: "已使用"
        url: ""
    }]


    $httpBackend.whenGET(/customer\/card/).respond (method, url, data) ->
        [200, {
            status: 0
            error: ''
            body: cardList
        }]

    chargeList = [{
        date: "2015.10.10"
        way: "在线充值"
        money: '300.00'
    },{
        date: "2015.13.11"
        way: "充值卡充值"
        money: '4000.00'
    }]
    $httpBackend.whenGET(/customer\/chargeHistory/).respond (method, url, data) ->
        [200, {
            status: 0
            error: ''
            body: chargeList
        }]

    $httpBackend.whenGET(/customer\/serviceIntroduce\/.*/).respond (method, url, data) ->
        id = parseInt url.match(/\d+$/)[0]
        name = if id == 7 then '周期订单' else '快速清洁'
        [200, {
            "status": 0
            "error": ""
            "body": {
                "serviceName": '周期订单',
                "serviceObject": "2房1厅1卫1厨1阳台，3小时快速清洁",
                "serviceRang": "日常范围家庭室内保洁（不适用于新装修房拓荒宝洁、公共区域、办公场所、商铺、宿舍）为了让您拥有更好的服务性价比，建议您参考以下标准进行人员预订。建议预订人数：80-100平/1人、100-180平/2人、180-300平/3人",
                "serviceBanner": [
                    "img/home/slide-1.png"
                    "img/home/slide-2.png"
                    "img/home/slide-3.png"
                ]
            }
        }]

    $httpBackend.whenGET(/customer\/personalInfo/).respond (method, url, data) ->
        [200, {
            status: 0
            error: ""
            body: {
                tel: '13688888888'
                name: "张爱国"
                sex: "男"
                address:
                    province:'上海'
                    city:'上海市'
                    region:'浦东新区'
            }
        }]

    $httpBackend.whenGET(/customer\/user\/center/).respond (method, url, data) ->
        [200, {
            status: 0
            error: ""
            body: {
                userInfo:
                    trueName: "张爱国"
                    mobilePhone: '13688888888'
                    gender:0
                cardNumber:2
                address:
                    province:'上海'
                    city:'上海市'
                    district:'浦东新区'
                    address:'我是详细地址'
                balance:'400.00'
            }
        }
        ]

    $httpBackend.whenGET(/customer\/product\?city=.*/).respond (method, url, data) ->
        [200, {
            "status": 0
            "error": ""
            "body":
              [
                  {
                      "id": 1,
                      "serviceIcon": "img/home/ksqj-icon.png",
                      "name": '快速快速清洁清洁',
                      "brief": '',
                      "isHot": true
                  },
                  {
                      "id": 2,
                      "serviceIcon": "img/home/ksqj-icon.png",
                      "name": '深度清洁',
                      "brief": '',
                      "isHot": true
                  },
                  {
                      "id": 3,
                      "serviceIcon": "img/home/ksqj-icon.png",
                      "name": '空调清洗',
                      "brief": '',
                      "isHot": true
                  },
                  {
                      "id": 4,
                      "serviceIcon": "img/home/ksqj-icon.png",
                      "name": '服务名称1',
                      "brief": '',
                      "isHot": true
                  },
                  {
                      "id": 5,
                      "serviceIcon": "img/home/ksqj-icon.png",
                      "name": '周期订单',
                      "brief": '',
                      "isHot": true
                  },
                  {
                      "id": 6,
                      "serviceIcon": "img/home/ksqj-icon.png",
                      "name": '其他产品',
                      "brief": '',
                      "isHot": true
                  },
              ]
        }
        ]

    $httpBackend.whenGET(/customer\/product\/.*/).respond (method, url, data) ->
        [200, {
            "status": 0
            "error": ""
            "body": {
                "id": 1,
                "name": "周期订单",
                "brief": "",
                "isHot": false,
                "productNo": "",
                "serviceTime": 1,
                "isPeriodic":true,
                "products": [
                    {
                        brief: "空调清洗"
                        descs: []
                        id: 11
                        isCycleProduct: false
                        isHot: false
                        name: "空调清洗"
                        productNo: "456747"
                        serviceIcon: ""
                        serviceObject: ""
                        serviceRange: ""
                        serviceTime: 0
                        attrs:[
                            "type": 1
                            "group": [
                                {
                                    id: '1'
                                    name: '1人99元'
                                    price: 99
                                    desc: '100平米一下'
                                    icon_off: 'img/order-commit/payWayIcon.png'
                                    amount:0
                                },
                                {
                                    id: '2'
                                    name: '2人134元'
                                    price: 199
                                    desc: '100平米~130平米'
                                    icon_off: 'img/tab-orders/clean.png'
                                    amount:0
                                }
                            ]
                        ]
                        strategies:[
                            {
                                displayStart: "8"
                                expireTime: "2016-08-24 14:47:39"
                                start: "7.98"
                                startTime: "2015-06-16 19:18:00"
                                type: 1
                                value: 80
                            }
                            {
                                displayStart: "5"
                                expireTime: "2016-08-24 14:48:49"
                                start: "4.98"
                                startTime: "2015-06-10 13:26:00"
                                type: 1
                                value: 50
                            }
                            {
                                displayStart: "3"
                                expireTime: "2016-08-24 14:49:22"
                                start: "2.98"
                                startTime: "2015-08-24 14:49:22"
                                type: 1
                                value: 20
                            }
                        ]
                    },
                    {
                        brief: "空调清洗"
                        descs: []
                        id: 12
                        isCycleProduct: false
                        isHot: false
                        name: "空调清洗2"
                        productNo: "456747"
                        serviceIcon: ""
                        serviceObject: ""
                        serviceRange: ""
                        serviceTime: 0
                        attrs:[
                            "type": 2
                            "group": [
                                {
                                    id: '3'
                                    name: '1人99元'
                                    price: 99
                                    desc: '100平米一下'
                                    icon_off: 'img/order-commit/payWayIcon.png'
                                    amount:0
                                },
                                {
                                    id: '4'
                                    name: '2人134元'
                                    price: 199
                                    desc: '100平米~130平米'
                                    icon_off: 'img/tab-orders/clean.png'
                                    amount:0
                                }
                            ]
                        ]
                        strategies:[
                            {
                                displayStart: "8"
                                expireTime: "2016-08-24 14:47:39"
                                start: "7.98"
                                startTime: "2015-06-16 19:18:00"
                                type: 1
                                value: 80
                            }
                            {
                                displayStart: "5"
                                expireTime: "2016-08-24 14:48:49"
                                start: "4.98"
                                startTime: "2015-06-10 13:26:00"
                                type: 1
                                value: 50
                            }
                            {
                                displayStart: "3"
                                expireTime: "2016-08-24 14:49:22"
                                start: "2.98"
                                startTime: "2015-08-24 14:49:22"
                                type: 1
                                value: 20
                            }
                        ]
                    },
                    {
                        brief: "空调清洗"
                        descs: []
                        id: 13
                        isCycleProduct: false
                        isHot: false
                        name: "空调清洗3"
                        productNo: "456747"
                        serviceIcon: ""
                        serviceObject: ""
                        serviceRange: ""
                        serviceTime: 0
                        attrs:[
                            "type": 3
                            "group": [
                                {
                                    id: '5'
                                    name: '1人99元'
                                    price: 99
                                    desc: '100平米一下'
                                    icon_off: 'img/order-commit/payWayIcon.png'
                                    amount:0
                                },
                                {
                                    id: '6'
                                    name: '2人134元'
                                    price: 199
                                    desc: '100平米~130平米'
                                    icon_off: 'img/tab-orders/clean.png'
                                    amount:0
                                }
                            ]
                        ]
                        strategies:[
                            {
                                displayStart: "8"
                                expireTime: "2016-08-24 14:47:39"
                                start: "7.98"
                                startTime: "2015-06-16 19:18:00"
                                type: 1
                                value: 80
                            }
                            {
                                displayStart: "5"
                                expireTime: "2016-08-24 14:48:49"
                                start: "4.98"
                                startTime: "2015-06-10 13:26:00"
                                type: 1
                                value: 50
                            }
                            {
                                displayStart: "3"
                                expireTime: "2016-08-24 14:49:22"
                                start: "2.98"
                                startTime: "2015-08-24 14:49:22"
                                type: 1
                                value: 20
                            }
                        ]
                    }
                ],
                "descs": [
                    {"content": ""}
                ]
                "strategies":[
                    {
                         id:1
                         isCheck:true
                         times:5
                         discount:0.95
                         discountDisplay:'9.5折'
                    },
                    {
                     id:2
                     isCheck:false
                     times:10
                     discount:0.9
                     discountDisplay:'9折'
                    },
                    {
                     id:3
                     isCheck:false
                     times:55
                     discount:0.55
                     discountDisplay:'5.5折'
                    }
            ]
            }
        }]

    $httpBackend.whenPOST(/customer\/comment/).respond (method, url, data) ->
        [200, {status: 0, error: ""}]

    $httpBackend.whenGET(/customer\/order\/.*/).respond (method, url, data) ->
        [200, {
            "status": 0
            "error": ""
            "body": {
                "productName": "快速清洁",
                "productId": 1,
                "orderNo": "sh20150012984850",
                "orderStatus": "派单中",
                "orderStatusCode": "",
                "province": "山东省",
                "city": "青岛市",
                "district": "浦东新区",
                "address": "沼泥庄一条马路",
                "totalPrice": 89,
                "year": 2015,
                "month": 5,
                "day": 1,
                "week": "",
                "timeQuantum": "12:00-15:00",
                "storeName": "",
                "storeId", 1,
                "customPhone": "1525758949",
                "customName": "张先生",
                "needComment": false
                "remark": "",
                "comment":"", #评价
                "signUrl":"",
                "attrs": [
                    {
                        "attrValue": "",
                        "attrName": "搽玻璃"
                    }
                ]
                "logs": [
                    {
                        "operatorType":"客户",
                        "operatorName": "张先生",
                        "time": "2015-05-24 18:38:00",
                        "description": "",
                        "status": 1,
                        "statusName": "已派单",
                    },
                    {
                        "operatorType":"悦管家",
                        "operatorName": "张经理",
                        "time": "2015-05-25 18:38:00",
                        "description": "",
                        "status": 2,
                        "statusName": "已接单",
                    }
                ]
            }
        }]

    $httpBackend.whenGET(/customer\/avaliable/).respond (method, url, data) ->
        status = ['disable', 'enable']
        makeTimeData = (i) ->
            if i < 10 then i = "0#{i}"
            # {range: '08:00~11:00' , status: s[(0+j)%2].type, statusText: s[(0+j)%2].text, value:0}
            [{range: "#{i}:00", status: status[_.random 1], value: 0}, {range: "#{i}:30", status: status[_.random 1], value: 0}]
        makeTimeList = -> _.flatten (makeTimeData(i) for i in [8..18])

        makeData = (i) ->
            if i < 10 then i = "0#{i}"
            {year:2015, month: '05', date: i, weekName: '周一',times:makeTimeList()}
        [200, {
            status: 0
            error: ""
            body: makeData(i) for i in [0..7]
        }]

    $httpBackend.whenGET(/customer\/region/).respond (method, url, data) ->
        [200, {
            status: 0
            error: ""
            body: [{
                code: '0000',
                name: '山东'
            }, {
                code: '0001',
                name: '山东1'
            }, {
                code: '00020',
                name: '上海市'
            }, {
                code: '00003',
                name: '山东3'
            }]
        }]
    $httpBackend.whenGET(/customer\/cycle-order/).respond (method, url, data) ->
        [200, {
            status: 0
            error: ""
            body: [
                {
                id:1
                totalTime:10
                leftTime:5
                needPay:false
                needUse:true
                }
            ]
        }]

    $httpBackend.whenGET(/customer\/order$/).respond (method, url, data) ->
        [200, {
            status: 0
            error: ""
            body: [{
                "orderId":12
                "productName":"快速清洁"
                "productId":1
                "orderNo":"no11111111111"
                "orderStatus":"已派单"
                "orderStatusCode":"4"
                "province":"上海"
                "city":"上海市"
                "district":"浦东新区"
                "address":"一条马路123号"
                "totalPrice":'99.00'
                "year":2015
                "month":5
                "day":1
                "week":"周一"
                "timeQuantum":"11:00-15:00"
                "storeName":"什么门店"
                "storeId":1
                "customPhone":"12345678901"
                "customName":"斯大林"
                "needDeparture":true
                "needServe":false
                "needUpload":true
                "bonusToken": '2a333b4d'
            }, {
                "orderId":13
                "productName":"快速清洁"
                "productId":1
                "orderNo":"no11111111111"
                "orderStatus":"已完成"
                "orderStatusCode":"4"
                "province":"上海"
                "city":"上海市"
                "district":"浦东新区"
                "address":"一条马路123号"
                "totalPrice":'90.09'
                "year":2015
                "month":5
                "day":1
                "week":"周六"
                "timeQuantum":"0"
                "storeName":"什么门店"
                "storeId":1
                "customPhone":"12345678901"
                "customName":"虾米"
                "needDeparture":false
                "needServe":false
                "needUpload":true
                "bonusToken": ''
            }]
        }]
    $httpBackend.whenPUT(/customer\/user/).respond (method, url, data) ->
        [200, {
            status: 0
            error: ""
        }]
    $httpBackend.whenGET(/customer\/orderDetailPeriodic/).respond (method, url, data) ->
        [200, {
            status: 0
            body: {
                id:'ZQB9987689'
                needPay:true
                Title:"周期订单包 10次"
                Times:'10'
                AvailableTimes:'0'
                address:'苦境 非马梦焉 23333弄'
                contact:'鱻生'
                tel:'2333333333'
                price:'233.00'
                remark:'非吾小天下，才高而已；非吾纵古今，时赋而已；非吾睨九州，宏观而已；三非焉罪？无梦至胜。'
            }
        }]

    $httpBackend.whenGET(/api\/bonus\/info/).respond (method, url, data) ->
        [200, {
            status: 0
            error: ""
            body: {
                senderImg: 'http://usr.im/200x200'
            }
        }]

    $httpBackend.whenPOST(/api\/bonus\/get/).respond (method, url, data) ->
        [200, {
            status: 0
            error: ""
            body: {
                receiver: '方晓青'
                name: '10元优惠券'
                desc: '可抵10元消费'
            }
        }]

    $httpBackend.whenGET(/.*/).passThrough()
